<template>
	<view class="page-height">
		<i-top-bar bgOpacity="0" :fixed="false"></i-top-bar>
		<image src="@/static/images/login-bg.png" class="bg"></image>
		<view class="page-content">
			<image src="@/static/images/logo02.png" class="logo"></image>

			<view class="form-view">
				<view class="col">
					<view class="item">
						<text class="label">手机号</text>
						<view class="line">
							<view class="input-bg">
								<input type="text" v-model="mobile" placeholder="请输入手机号" class="input" />
							</view>
						</view>
					</view>
					<view class="item">
						<text class="label">验证码</text>
						<view class="line">
							<view class="input-bg">
								<input type="text" v-model="sms_code" placeholder="请输入验证码" class="input" />
							</view>
							<text class="get-sms" @click="onGetSms()" v-if="time >= 60">{{i18n_t('获取验证码')}}</text>
							<text class="get-sms" v-else>{{time}}s</text>
						</view>
					</view>

				</view>

				<button class="btn" @click="onLogin()">登 录</button>


			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
		<i-toast ref="iToast"></i-toast>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				timer: null,
				time: 60,
				mobile: "",
				sms_code: "",
			};
		},


		methods: {
			onGetSms() {
				if (!this.mobile) return this.fail("请输入手机号")
				if (this.time < 60) return false

				const params = {
					mobile: `${this.mobile}`,
					event: "mobilelogin"
				}
				if (this.isRequest()) return false;
				this.api.post("/api/sms/send", { params: params }).then(res => {
					uni.showToast({
						title: "发送成功"
					})
					clearInterval(this.timer)
					this.timer = setInterval(() => {
						if (this.time > 0) {
							this.time--
						} else {
							clearInterval(this.timer)
							this.time = 60
						}
					}, 1000)
				}).catch(err => {
					this.apiErr(err)
				}).finally(() => {
					this.loadingHide();
				})

			},
			onLogin() {
				if (!this.mobile) return this.fail("请输入手机号")
				if (!this.sms_code) return this.fail("请输入短信验证码")

				const params = {
					mobile: this.mobile,
					captcha: this.sms_code,
				}
				this.api.post("/api/user/mobilelogin", { params: params }).then(res => {
					const token = res.userinfo.token
					this.$store.commit('login', token)
					this.getUserInfo();
					uni.showToast({
						title: "登录成功"
					})
					setTimeout(() => {
						uni.switchTab({
							url: "/pages/home/index"
						})
					}, 1000)
				}).catch(err => {
					this.apiErr(err)
				}).finally(() => {
					this.loadingHide();
				})
			},
			onOpenLogin() {
				uni.reLaunch({
					url: "/pages/login/login-pass"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-height {
		background: #ffffff;
	}

	.page-content {
		display: flex;
		flex-direction: column;
		position: relative;
		z-index: 1;
		padding-bottom: 30rpx;

	}

	.bg {
		width: 750rpx;
		height: 436rpx;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 0;
	}

	.logo {
		max-width: 180rpx;
		max-height: 180rpx;

		margin: 110rpx auto 0;
	}

	.form-view {
		display: flex;
		flex-direction: column;
		background-color: #ffffff;
		border-radius: 30rpx 30rpx 0rpx 0rpx;
		margin-top: 60rpx;
		padding: 20rpx 70rpx 0;

		.title {
			font-size: 42rpx;
			color: #333333;
			margin-top: 54rpx;
		}

		.col {
			display: flex;
			flex-direction: column;

			.item {
				display: flex;
				flex-direction: column;

				.label {
					font-size: 32rpx;
					color: #333333;
					margin-top: 32rpx;
				}

				.line {
					display: flex;
					align-items: center;
					margin-top: 20rpx;

					.input-bg {
						height: 98rpx;
						border-radius: 100px;
						flex: 1;
						background: #F7F8FA;
						border-radius: 100px;
						display: flex;
						align-items: center;
						padding: 0 32rpx;

						.input {
							flex: 1;
						}
					}

					.get-sms {
						width: 220rpx;
						height: 98rpx;
						border-radius: 100px;
						margin-left: 24rpx;
						background: #55C29A;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 32rpx;
						color: #FFFFFF;
					}
				}
			}
		}
	}

	.btn {
		margin-top: 100rpx;
	}
</style>